package org.bpms.service.admin;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.bpms.model.admin.Book;
import org.bpms.model.admin.Btype;

import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.tx.Tx;

/**
 * 档案操作接口
 * @author hupei
 *
 */
public class BookService {
	
	/**
	 * 保存 
	 */
	@Before(Tx.class)
	public void save(Book temp){
		if (null == temp.get("id")){
			temp.set("id", UUID.randomUUID().toString().replaceAll("-", ""));
			temp.save();
		}
	}
	/**
	 * 分页查找正在处理的
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllDoing(String role , String title ,String flow_code , String file_number , String uid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book where 1=1");
		sql.append(" and creater='"+uid+"'");
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and flow_code like '%"+flow_code+"%'");
		}
		if (role.equals("资源收集岗")){
			sql.append(" and state='A'");
		}else if  (role.equals("资源开发岗")){
			sql.append(" and state='D'");
		}else if  (role.equals("资源利用岗")){
			sql.append(" and state='F'");
		}else if  (role.equals("科领导")){
			sql.append(" and state='D'");
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}

	/**
	 * 分页查找 等待处理
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllWait(String role , String title , String flow_code , String file_number , String uid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book where 1=1");
		sql.append(" and creater='"+uid+"'");
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and flow_code like '%"+flow_code+"%'");
		}
		if  (role.equals("资源开发岗")){
			sql.append(" and state='C'");
		}else if  (role.equals("科领导")){
			sql.append(" and state='C'");
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}
	/**
	 * 分页查找 已提交的
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllSubmit(String role , String flow_code , String title , String file_number , String uid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book where 1=1");
		sql.append(" and creater='"+uid+"'");
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and flow_code like '%"+flow_code+"%'");
		}
		if  (role.equals("资源开发岗")){
			sql.append(" and state='E'");
		}else if  (role.equals("科领导")){
			sql.append(" and state='E'");
		}else if  (role.equals("资源收集岗")){
			sql.append(" and state='B'");
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}	
	
	/**
	 * 分页查找 待接收的
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllWaitGet(List<Btype> types , String role , String flow_code , String title , String file_number , String uid , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book where 1=1");
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and flow_code like '%"+flow_code+"%'");
		}
		if  (role.equals("资源开发岗")){
			sql.append(" and state='B'");
		}else if  (role.equals("科领导")){
			sql.append(" and state='B'");
		}else if  (role.equals("资源利用岗")){
			sql.append(" and state='E'");
		}
		if (null != types && types.size() > 0){
			if (types.size() == 1){
				sql.append(" and typeid='"+types.get(0).getStr("id")+"'");
			}else{
				for (int i = 0 ; i < types.size() ; i++){
					if (i == 0){
						sql.append(" and typeid in('"+types.get(i).getStr("id")+"'");
					}else if(i == types.size()-1){
						sql.append(",'"+types.get(i).getStr("id")+"')");
					}else{
						sql.append(",'"+types.get(i).getStr("id")+"'");
					}
				}				
			}
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}
	
	/**
	 * 查询所有档案
	 * @param title
	 * @param flow_code
	 * @param file_number
	 * @param uid
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllDoc(String role , String title ,String flow_code , String file_number , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book a where 1=1");
		StringBuffer sql1 = new StringBuffer(" from book a where 1=2");
		String sqltemp = "";
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and a.file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and a.flow_code like '%"+flow_code+"%'");
		}
		sql.append(" order by "+orderField+" "+orderDirection);
		if (title.equals("")&&file_number.equals("")&&flow_code.equals("")){
			if  (role.equals("科领导")){
				sqltemp = sql.toString();
			}else{
				sqltemp = sql1.toString();
			}
		}else{
			sqltemp = sql.toString();
		}
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sqltemp);
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}
	
	/**
	 * 获取30天内已完成的档案
	 * @param uid
	 * @param title
	 * @param flow_code
	 * @param file_number
	 * @param pageNumber
	 * @param pageSize
	 * @param orderField
	 * @param orderDirection
	 * @param pagemap
	 */
	public void findAllCompleted(String uid , String title ,String flow_code , String file_number , Integer pageNumber,Integer pageSize,
	           String orderField,String orderDirection,Map<String,Object> pagemap){
		StringBuffer sql = new StringBuffer(" from book a where 1=1");
		sql.append(" and creater='"+uid+"' and state='G'");
		if (!title.equals("")){
			String [] titles = title.split(" ");
			if (null != titles && titles.length > 0){
				for (String t : titles){
					sql.append(" or title like '%"+t+"%'");
				}
			}
		}
		if (!file_number.equals("")){
			sql.append(" and a.file_number like '%"+file_number+"%'");
		}
		if (!flow_code.equals("")){
			sql.append(" and a.flow_code like '%"+flow_code+"%'");
		}
		sql.append(" and date_sub(curdate(), INTERVAL 30 DAY) <= date(ctime)");
		sql.append(" order by "+orderField+" "+orderDirection);
		Page<Book> ps = Book.me.paginate(pageNumber, pageSize, "select * ", sql.toString());
		pagemap.put("pageSize", ps.getPageSize());//页面大小
		pagemap.put("totalRow", ps.getTotalRow());//总行数
		pagemap.put("pageCurrent", ps.getPageNumber());//当前页号
		pagemap.put("orderField", orderField);//排序列名
		pagemap.put("orderDirection", orderDirection);//排序方式
		pagemap.put("pageData", ps.getList());//页面数据
	}	
}
